import java.io.*;

public class Product implements Serializable
{
  protected String packaging;
  protected boolean reserved;
  protected boolean sold;
  protected String  color;
  protected Clothes article;
  protected int     price;

  // "offical" constructor - no arguments!
  // so we need setters even for the "non-public" properties (price etc)
  public Product() {
    try {
      Product prod = loadProduct();
      packaging = prod.retrievePackaging();
      price = prod.getPrice();
      article = prod.getArticle();
      color = prod.getColor();
      reserved = prod.isReserved();
      sold = prod.isSold();
    } catch (Exception ex) {
      packaging = "box";
      price = 0;
      article = new Clothes("noname");
      color ="transparent";
      reserved = false;
      sold = false;
    }
  }   

  public void setReserved(boolean r) {
    reserved = r;
  }

  public boolean isReserved() {
    return reserved;
  }

  public void setSold(boolean s) {
    sold = s;
  }

  public boolean isSold() {
    return sold;
  }

  public void setColor(String c) {
    color = c;
  }

  public String getColor() {
    return color;
  }

  public void setArticle(Clothes a) {
    article = a;
  }

  public Clothes getArticle() {
    return article;
  }

  public void setPrice(int p) {
    price = p;
  }

  public int getPrice() {
    return price;
  }

  public void customizePackaging(String pack) {
    packaging = pack;
  }

  public String retrievePackaging() {
    return packaging;
  }

  private void writeObject(ObjectOutputStream oos) throws IOException {
    oos.defaultWriteObject();
    oos.writeObject("Author: Frank");
  }

  private void readObject(ObjectInputStream ois)
    throws IOException, ClassNotFoundException
  {
    String author;

    ois.defaultReadObject();
    author = (String)ois.readObject();
    // reject classes from unknown authors
    if (!author.equals("Author: Frank")) throw new ClassNotFoundException();
  }

  public void saveProduct() {
    try {
      FileOutputStream fos = new FileOutputStream("Product.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);

      oos.writeObject(this);
      oos.close();
    } catch (IOException iox) {
        System.err.println("IO error while saving Product: " + iox);
    }
  }
  
  public Product loadProduct() throws IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream("Product.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Product prod = (Product)ois.readObject();
    ois.close();
    return prod;
  }
}   
